<?php

/**
 * Class Weixin_Admin_module
 */
class Weixin_Worder_module extends CI_Module
{
    public $weixin_model;
    public $setting;
    public $sid ;

    public function __construct()
    {
        parent::__construct();
        $this->sid = $GLOBALS['sid'];
        $this->load->model('weixin.weixin_model');
        $this->load->model('expert.Expert_model');
        $this->load->model('weixin.Worder_tel_model');
        $this->setting = $this->weixin_model->db_get_setting();
        define('TOKEN',     @$this->setting['token']);
        define('APPID',     @$this->setting['appid']);
        define('APPSECRET', @$this->setting['appsecret']);
        $this->adminid = $this->session->userdata('admin_user_id');
        if(!$this->adminid){
            redirect(site_url('admin/index/login'));
        }
    }

    public function index()
    {
        $condition['keywords']    = addslashes($this->input->get_post('keywords'));
        $condition['start_date']  = addslashes($this->input->get_post('start_date'));
        $condition['end_date']    = addslashes($this->input->get_post('end_date'));
        $condition['status']      = $this->input->get_post('status');

        if($condition['status']===false)$condition['status']=-1;
        $where = "";
        if($condition['keywords']){
            $where .= " and line_name like'%".$condition['keywords']."%' or tel='".$condition['keywords']."'";
        }
        if($condition['start_date']){
            $where .= " and add_time>='".$condition['start_date']."'";
        }
        if($condition['end_date']){
            $where .= " and add_time<='".$condition['end_date']." 23:59:59'";
        }
        if($condition['status']==0||$condition['status']==1){
            $where .= " and status=$condition[status] ";
        }

        $page_config['perpage'] =20;   //每页条数
        $page_config['url']     ='weixin/worder/index';//url
        $page_config['param'] = '?keywords='.$condition['keywords'].'&start_date='.$condition['start_date'].'&end_date='.$condition['end_date'];//参数
        $page_config['seg']     =4;//参数取 index.php之后的段数，默认为3，即index.php/control/function/18 这种形式
        $page_config['nowindex']=$this->uri->segment($page_config['seg']) ? $this->uri->segment($page_config['seg']):1;//当前页
        $result            = $this->Worder_tel_model->get_list($page_config['perpage'],$page_config['nowindex'],$where);
        $page_config['total']   = $result['count'];
        $this->load->library('mypageclass');
        $this->mypageclass->initialize($page_config);
        $data['list']      = $result['list'];
        $data['index_url'] = site_url('weixin/worder/index');
        $data['condition'] = $condition;
        $data['sid'] = $this->sid;
        $data['title'] = '订单管理-预定电话';
        $data['view'] = 'worder_tel';
        $this->loadview($data, 'xenon/common/index');

    }

    /**
     * 电话预定处理页面
     * @param $id
     */
    public  function edit_tel($id){
        $data['title'] = '订单管理-预定电话-处理';
        $data['view']  = 'worder_edit_tel';
        $data['save_url'] = site_url('weixin/worder/save_tel');
        $data['row']   = $this->Worder_tel_model->get_row(array('id'=>$id));
//        print_r($data['row']);exit;


        $this->loadview($data, 'xenon/common/index');
    }

    public function save_tel(){
        $id = $this->input->get_post('id');
        $remark = $this->input->get_post('remark');
        $status = $this->input->get_post('status');
        $data = array(
            'remark'=>$remark,
            'status'=>$status,
            'up_time'=>date('Y-m-d H:i:s'),
            );
        $res = $this->Worder_tel_model->edit_row($data,array('id'=>$id));
        if($res){
            $this->success('操作成功',site_url('weixin/worder/index'),true);
        }else{
            $this->error('操作失败','',true);
        }

    }


}